<template>
	<view class="container">
		<button @click="getIPLocation">获取IP定位信息</button>
		<view v-if="location">
			<!-- 			<view>IP定位信息：</view>
			<view>省份：{{ ipLocationInfo.province }}</view>
			<view>城市：{{ ipLocationInfo.city }}</view>
			<view>区域编码(adcode)：{{ ipLocationInfo.adcode }}</view>
			<view>矩形区域范围：{{ ipLocationInfo.rectangle }}</view> -->
			<view>纬度：{{ location.latitude }}</view>
			<view>经度：{{ location.longitude }}</view>
			<button @click="getWeather">获取天气信息</button>
			<view v-if="weatherInfo">
				<view>当前天气：{{ weatherInfo.weather }}</view>
				<view>温度：{{ weatherInfo.temperature }}℃</view>
				<view>风向：{{ weatherInfo.winddirection }}</view>
				<view>风力：{{ weatherInfo.windpower }}</view>
				<view>湿度：{{ weatherInfo.humidity }}%</view>
			</view>
			<button @click="getReGeocode">逆地理编码</button>
			<view v-if="addressInfo">
				<view>地理位置信息：</view>
				<view>国家：{{ addressInfo.addressComponent.country }}</view>
				<view>省：{{ addressInfo.addressComponent.province }}</view>
				<view>市：{{ addressInfo.addressComponent.city }}</view>
				<view>区县：{{ addressInfo.addressComponent.district }}</view>
				<view>街道：{{ addressInfo.addressComponent.township }}</view>
			</view>
		</view>
		<button @click="chooseLocation">选择地址</button>
		<view v-if="chooseInfo">
			<view>选择地址：{{ chooseInfo}}</view>
		</view>
		<button @click="chooseMedia">拍摄或者选择图片</button>
		<view v-if="img">
			<view><image :src="img"></image></view>
		</view>
		
	</view>
</template>
<script setup>
	import {
		ref,
		reactive
	} from 'vue';

	const ipLocationInfo = ref(null);
	const weatherInfo = ref(null);
	const addressInfo = ref(null);
	const chooseInfo=ref(null);
	const img=ref(null);
	const ak = '171f867864d97f780e5c0c6be10341db'; // 替换为自己的高德API Key

	let city = ''; // 用于存储IP定位获取到的城市信息
	// let location = ''; // 用于存储经纬度信息

	const location = reactive({
		latitude: '',
		longitude: ''
	});

	const getIPLocation = () => {
		console.log("开始查找地址");
		wx.getLocation({
			type: 'gcj02',
			isHighAccuracy: true,
			success(res) {
				console.log(res);
				location.latitude=limitDecimalPlaces(res.latitude);
				location.longitude=limitDecimalPlaces(res.longitude);
				// location.latitude = res.latitude;
				// location.longitude = res.longitude;
				console.log(location)
			},
			fail: (err) => {
				console.error('请求失败:', err);
			}
		})
	};

	const getWeather = () => {
		// if (!ipLocationInfo.value || !ipLocationInfo.value.city) {
		// 	uni.showToast({
		// 		title: '请先获取IP定位信息',
		// 		icon: 'none'
		// 	});
		// 	return;
		// }

		city = addressInfo.value.addressComponent.city; // 从ipLocationInfo中获取城市信息
		console.log(city)
		uni.request({
			url: `https://restapi.amap.com/v3/weather/weatherInfo?key=${ak}&city=${city}`,
			method: 'GET',
			success: (res) => {
				console.log(res.data);
				if (res.statusCode === 200 && res.data.status === '1') {
					weatherInfo.value = res.data.lives[0];
				} else {
					console.error('获取天气失败:', res.data.info);
				}
			},
			fail: (err) => {
				console.error('请求天气接口失败:', err);
			}
		});
	};
	//location 经纬度  ak 高德api key
	const getReGeocode = () => {
		if (!location) {
			uni.showToast({
				title: '请先获取IP定位信息',
				icon: 'none'
			});
			return;
		}
		uni.request({
			url: `https://restapi.amap.com/v3/geocode/regeo?location=${location.longitude},${location.latitude}&key=${ak}&extensions=all`,
			method: 'GET',
			success: (res) => {
				console.log("逆编码状态：" + res.data.status);
				if (res.statusCode === 200 && res.data.status === '1') {
					addressInfo.value = res.data.regeocode;
				} else {
					console.error('逆地理编码失败:', res.data.info);
				}
			},
			fail: (err) => {
				console.error('请求失败:', err);
			}
		});
	};
	
	const limitDecimalPlaces = (num) => {
		// 将数字转换为字符串，以便于操作
		let numStr = num.toString();

		// 检查是否包含小数点
		if (numStr.indexOf('.') !== -1) {
			// 分割整数部分和小数部分
			let parts = numStr.split('.');

			// 如果小数部分长度大于6，则截断为6位小数
			if (parts[1] && parts[1].length > 6) {
				// 截断小数部分为6位，并与整数部分重新组合
				numStr = parts[0] + '.' + parts[1].substring(0, 6);
			}
		}
		// 将处理后的字符串转换回数字
		return parseFloat(numStr);
	}
	
	const chooseLocation =()=>{
		console.log("开始选择")
		wx.chooseLocation({
			success(res) {
				chooseInfo.value=res.address
			}
		})
	}
	
	const chooseMedia =()=>{
		wx.chooseMedia({
		  count: 1,
		  mediaType: ['image','video'],
		  sourceType: ['album', 'camera'],
		  maxDuration: 30,
		  camera: 'back',
		  success(res) {
		    console.log(res.tempFiles[0].tempFilePath)
			img.value=res.tempFiles[0].tempFilePath;
		    console.log(res.tempFiles[0].size)
		  }
		})
	}
</script>
<style scoped>
</style>
